// ignore_for_file: avoid_print

import 'package:flujs/flujs.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flujs_jsc/jsc.dart' as jsc;
import 'package:flujs_qjs/qjs.dart' as qjs;

void main() {
  timeout(JSFRuntime rt) async {
    var ctx = rt.newContext()..loadExtension();
    final id = ctx.eval('''
setTimeout(() => console.log('done!'), 300)
console.log('hhhh')
''');
    print('[timeout] $id');
    await Future.delayed(const Duration(milliseconds: 1500));
    // FIXME: quickJS free Runtime will assert failed: Shell: Assertion failed: (list_empty(&rt->gc_obj_list)), function JS_FreeRuntime, file quickjs.c, line 1971.
    // ctx.free();
    // if (ctx.isQJS) {
    //   (ctx.runtime.rt as JSRuntime).gc();
    // }
    // rt.free();
  }

  interval(JSFRuntime rt) async {
    var ctx = rt.newContext()..loadExtension();
    ctx.eval('''
setInterval(() => console.log('tictok!'), 500);
''');
    await Future.delayed(const Duration(seconds: 3));
    // ctx.free();
    // if (ctx.isQJS) {
    //   (ctx.runtime.rt as JSRuntime).gc();
    // }
    // rt.free();
  }

  test('timeout_jsc', () async {
    print('[timeout__jsc]\n\n');
    await timeout(jsc.getJSFRuntime());
  });
  test('timeout_qjs', () async {
    print('[timeout__qjs]\n\n');
    await timeout(qjs.getJSFRuntime());
  });
  test('interval_jsc', () async {
    print('[interval__jsc]\n\n');
    await interval(jsc.getJSFRuntime());
  });
  test('interval_qjs', () async {
    print('[interval__qjs]\n\n');
    await interval(qjs.getJSFRuntime());
  });
}
